package com.completecompute;
import com.DTC.*;
import com.fourinone.Contractor;
import com.fourinone.WareHouse;
import com.fourinone.WorkerLocal;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class CtorDemo extends Contractor
{
	private String ctorname;
	private CopyOnWriteArrayList<D> dInList;
	private CopyOnWriteArrayList<D> dOutList;
	
	CtorDemo(String ctorname,CopyOnWriteArrayList<D> dInList,CopyOnWriteArrayList<D> dOutList)
	{
		this.ctorname = ctorname;
		this.dInList = dInList;
		this.dOutList = dOutList;
	}
	
	public WareHouse giveTask(WareHouse inhouse)
	{
		WorkerLocal[] wks = getWaitingWorkers("workdemo");
		System.out.println("wks.length:"+wks.length);
		
		WareHouse[] hmarr = new WareHouse[wks.length];
		for(int i=0;i<wks.length;i++){
			if(hmarr[i]==null){
				WareHouse wh = new WareHouse();
				D d = new D();
				wh.put("D",d);
				hmarr[i] = wks[i].doTask(wh);
			}
		}
		System.out.println("初始任务分配完成");
		try{
			Thread.sleep(1000);
		}catch(Exception e){
			System.out.println(e);
		}
		while(dInList.size()!=0){
			for (int i=0;i<wks.length;i++) {
				if(hmarr[i].getStatus()!=WareHouse.NOTREADY){
					if(((D)hmarr[i].getObj("D")).name != -1){
						D overD = (D)hmarr[i].getObj("D");
						for (D d : dInList) {
							if(d.name==overD.name){
								dInList.remove(overD);
								dOutList.add(overD);
								d.setState(2);
							}
						}
					}
					if(dInList.size()==0){
						inhouse.setString("id", "OK");
						return inhouse;
					}
					for(D d : dInList){
						// System.out.print(d.state);
						if(d.getState()==0){
							WareHouse wh = new WareHouse();
							wh.put("D",d);
							hmarr[i] = wks[i].doTask(wh);
							d.setState(1);
							break;
						}
					}
					// System.out.println(	((D)hmarr[i].getObj("D")).name);
				}
			}
			// D d = dInList.get(0);
			// d.gain = 10.0;
			// d.mid = 2.3;
			// d.setState(2);
			// dInList.remove(d);
			// dOutList.add(d);
			// System.out.println("完成"+d.getName());
			// System.out.println("任务长度"+dInList.size());
		}
		// //  
		// while(dInList.size()!=0){
		// 	for (int i=0;i<wks.length;i++) {
		// 		if(hmarr[i].getStatus()!=WareHouse.NOTREADY){
		// 			hmarr[i].getObjValues();
		// 			System.out.println(hmarr[i].getObjValues());
					
		// 		}	
		// 	}
		// }


		// 	// else if(hmarr[i].getStatus()!=WareHouse.NOTREADY)
		// 	// {

		// 	// 	// attrList.remove(j);
		// 	// 	System.out.println(hmarr[i]);
		// 	// 	outStr+=hmarr[i];
		// 	// 	hmarr[i]=null;
		// 	// 	j++;
		// 	// }
		// }
		// int j=0;
		// while(true){
		// 	for (int i=0;i<wks.length;i++) {
		// 		if(hmarr[i].getStatus()!=WareHouse.NOTREADY){
		// 			hmarr[i].getObjValues();
		// 			System.out.println(hmarr[i].getObjValues());
					
		// 		}	
		// 	}
		// 	j++;
		// 	if(j>10){
		// 		break;
		// 	}
		// 	try{
		// 		Thread.sleep(2000);
		// 	}catch(Exception e){

		// 	}
			
		// }
		
		inhouse.setString("id", "OK");
		return inhouse;
	}
	
	public static void main(String[] args)
	{
		CopyOnWriteArrayList<D> dInList = new CopyOnWriteArrayList<>();// 按属性划分的任务列表
		CopyOnWriteArrayList<D> dOutList = new CopyOnWriteArrayList<>();// 按属性划分的任务列表
		Contractor a = new CtorDemo("OneCtor",dInList,dOutList);
		// 维护一个任务消息队列和一个工人队列，对于任务消息队列的操作是互斥的
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run(){
				System.out.println("进入线程里");
				LoadData loadData = new LoadData("data/iris.data");
				ArrayList<Integer> attrList = new ArrayList<>();
				attrList.add(0);
				attrList.add(1);
				attrList.add(2);
				attrList.add(3);
				NodeDis root = new NodeDis(loadData.irisTrain, attrList ,dInList,dOutList);
				int i = 0;
        		for (IrisEntry data : loadData.irisTest) {
            		Flower a = NodeDis.getFlowerType(data.getDataByArray(), root);
            		System.out.println("预测:"+ a + "\t实际" + data.getFlowerType());
					if(a==data.getFlowerType()){
						i++;
					}
        		}
        		System.out.println("预测成功率"+(i/(loadData.irisTest.size()+0.0)));	
				return;			
			}
		});
		thread.start();

		WareHouse house = new WareHouse("id","begin ");
		System.out.println(a.giveTask(house));
	}
}


// try{
// 	Thread.sleep(1000);
// 	System.out.println(dInList.size());
// 	while(dInList.size()!=0){
// 		D d = dInList.get(0);
// 		d.gain = 10.0;
// 		d.mid = 2.3;
// 		d.setState(2);
// 		dInList.remove(d);
// 		dOutList.add(d);
// 		System.out.println("完成"+d.getName());
// 		System.out.println("任务长度"+dInList.size());
// 		Thread.sleep(10);
// 	}
// }catch(Exception e){
// 	System.out.println(e);
// }